package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import agents.AdverseryAgent;
import env.City;
import env.Move;
import env.World;

public class Main {
	// constants
	public static final int noOpCost = 1, hellBreakLooseCost = 100000000,
			cutoff = 10, goalScore=100;
	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int score=0;
		try {
			World world = utils.Parser.parseWorldFile("world.txt");
			AdverseryAgent agent1 = new AdverseryAgent(null, world);
			
			System.out.println("type initial city for agent1");
			String line;
			line = br.readLine();
			
			agent1.current = world.getCityByName(line);
			world.addAgent(agent1, agent1.current);
			System.out.println("type goal city for agent1");
			line = br.readLine();
			while(!(line.equals("DONE") || line.equals("")))
			{
				agent1.goals.add(world.getCityByName(line));
				System.out.println("type goal city for agent1 (or \"DONE\")");
				line = br.readLine();
				
			}
			AdverseryAgent agent2 = new AdverseryAgent(agent1, world);
			System.out.println("type initial city of agent2");
			line = br.readLine();
			agent2.current = world.getCityByName(line);
			world.addAgent(agent2, agent2.current);
			System.out.println("type goal city for agent2");
			line = br.readLine();
			while(!(line.equals("DONE")|| line.equals("")))
			{
				agent2.goals.add(world.getCityByName(line));
				System.out.println("type goal city for agent2 (or \"DONE\")");
				line = br.readLine();
				
			}
			boolean agent1_turn=true;
			
			while(!world.allDone())
			{
				//System.out.println("MOVING");
				if(agent1_turn)
				{
					City agent1Current = agent1.current;
					City agent2Current = agent2.current;
					Move move = agent1.calcMove();
					agent1.setCurrentCity(agent1Current);
					agent2.setCurrentCity(agent2Current);
					int score_change = world.moveAgents(move);
					agent1.score+=score_change;
					score+=score_change;
					System.out.println("1: " + move);
					agent1_turn =! agent1_turn;
					
					
				}
				else
				{
					City agent1Current = agent1.current;
					City agent2Current = agent2.current;
					Move move = agent2.calcMove();
					agent1.setCurrentCity(agent1Current);
					agent2.setCurrentCity(agent2Current);
					int score_change = world.moveAgents(move);
					agent2.score+=score_change;
					score-=score_change;
					System.out.println("2: "+move);
					agent1_turn =! agent1_turn;
				}
				System.out.println("Agent1 score:"+agent1.score+" Agent2 score:" +agent2.score);
//				if(score>0)
//					System.out.println("Agent2 leads by " +score);
//				else
//					System.out.println("Agent1 leads by " +score*-1);
			}
			System.out.println("GAME OVER\nAgent1 score:"+agent1.score+" Agent2 score:" +agent2.score);
			System.out.println("GAME OVER");
//			if(score>0)
//				System.out.println("Agent2 wins by " +score*2);
//			else
//				System.out.println("Agent1 wins by " +score*-2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
